<template>
  <div class="task-create">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>创建任务</span>
      </div>
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="预约人">
          <el-input v-model="form.appointmentPerson"></el-input>
        </el-form-item>
        <el-form-item label="所属客户">
          <el-input v-model="form.customer"></el-input>
        </el-form-item>
        <el-form-item label="经办人">
          <el-input v-model="form.processor"></el-input>
        </el-form-item>
        <el-form-item label="账号">
          <el-input v-model="form.appointmentPhone"></el-input>
        </el-form-item>
        <el-form-item label="验证码">
          <el-input style="width: 100px" v-model="form.authCode"></el-input>
          <el-button style="float: right" type="primary" @click="sendMessageAuthCode">发送验证码</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">立即创建</el-button>
          <el-button @click="cancel">取消</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>

</template>
<script>
import { appointmentTasks, sendMessageAuthCode } from '@/services/mother'

export default {
  name: 'TaskCreate',
  data () {
    return {
      form: {
        appointmentPerson: '',
        customer: '',
        processor: '',
        appointmentPhone: '',
        authKey: '',
        authCode: ''
      }
    }
  },
  methods: {
    async cancel () {
      await this.$router.push('/mother')
    },
    async onSubmit () {
      console.log(this.form)
      const { data } = await appointmentTasks({
        appointmentPerson: this.form.appointmentPerson,
        customer: this.form.customer,
        processor: this.form.processor,
        appointmentPhone: this.form.appointmentPhone,
        authKey: this.form.authKey,
        authCode: this.form.authCode
      })
      if (data.code === 200) {
        this.$message.success('任务创建成功')
        await this.$router.push('/mother')
      } else {
        this.$message.error('任务创建失败')
      }
    },
    async sendMessageAuthCode () {
      const { data } = await sendMessageAuthCode(this.form.appointmentPhone)
      if (data.code === 200) {
        this.$message.success('验证码发送成功')
        this.form.authKey = data.data.authKey
      } else {
        this.$message.error('验证码发送失败')
      }
    }
  }
}
</script>
<style scoped lang="scss">

</style>
